package com.ruoyi.project.cropmanage.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cropmanage.entity.CropMap;
import com.ruoyi.project.cropmanage.service.CropmapServiceImpl;
import com.ruoyi.project.cropmanage.service.ICropmapService;
import com.ruoyi.project.dataanalysis.entity.ParamsConvert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * huzhonghua
 * 2020/2/11
 * 12:23
 */
@Controller
public class CropmapController extends BaseController {
    @Autowired
    private ICropmapService cropmapService;

    private String prefix = "cropmanage";
    @GetMapping("/cropmanage/crop_map")
    public String crop_map()
    {
        return prefix+"/crop_map";
    }

    @PostMapping("/cropmanage/insert")
    @ResponseBody
    public AjaxResult crop_map_insert(CropMap cropmap)
    {
        return toAjax(cropmapService.insertCropmap(cropmap));
    }

    @PostMapping("/cropmanage/json")
    @ResponseBody
    public Map<String, Object> crop_map_getAll(@RequestBody Map<String, Object> map)
    {
        return cropmapService.crop_map_getAll(map);
    }

    @PostMapping("/cropmanage/jsonok")
    @ResponseBody
    public Map<String, Object> crop_map_getAllok(@RequestBody Map<String, Object> map)
    {
        return cropmapService.crop_map_getAllok(map);
    }

    @GetMapping("/cropmanage/detail")
    public String crop_detail()
    {
        return prefix+"/crop_detail";
    }

    @RequiresPermissions("crop:list")
    @PostMapping("/cropmanage/detaillist")
    @ResponseBody
    public TableDataInfo crop_detaillist()
    {   startPage();
        List<CropMap> detail= cropmapService.selectCropmapAll();
        return getDataTable(detail);
    }

    /**
     * 进入修改界面
     */
    @GetMapping("/cropmanage/edit/{cropId}")
    public String edit(@PathVariable("cropId") Long cropId, ModelMap mmap)
    {
        mmap.put("cropMap", cropmapService.selectCropmapById(cropId));
        return prefix + "/crop_edit";
    }

    @RequiresPermissions("cropmanage:crop_edit")
    @PostMapping("/cropmanage/edit")
    @ResponseBody
    public AjaxResult crop_edit(@Validated CropMap cropMap)
    {
        return toAjax(cropmapService.updateCropmap(cropMap));
    }

    @RequiresPermissions("crop:remove")
    @PostMapping("/cropmanage/remove")
    @ResponseBody
    public AjaxResult crop_remove(String ids)
    {
        Long[] tmp_ids = Convert.toLongArray(ids);
        try
        {
            return toAjax(cropmapService.deleteCropmapById(tmp_ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    @Log(title = "种植导出", businessType = BusinessType.EXPORT)
    @PostMapping("/cropmanage/export")
    @ResponseBody
    public AjaxResult export(CropMap cropmap)
    {
        List<CropMap> list = cropmapService.selectCropmapAll();
        ExcelUtil<CropMap> util = new ExcelUtil<CropMap>(CropMap.class);
        return util.exportExcel(list, "导出数据");
    }
}
